package com.flida.bana.common.util;
import com.feilong.core.bean.BeanOperationException;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * 通过地址获取高德地图的经纬度
 * Created by yzc on 2016/12/15.
 */
public class GaodeHelper {
    private static String API = "http://restapi.amap.com/v3/geocode/geo?key=<key>&s=rsv3&address=<address>";

    private static String KEY = "aa4a48297242d22d2b3fd6eddfe62217";

    private static Pattern pattern = Pattern.compile("\"location\":\"(\\d+\\.\\d+),(\\d+\\.\\d+)\"");

    static {
        init();
    }

    private static void init() {
        System.out.println("高德地图工具类初始化");
        System.out.println("api: {}"+API);
        System.out.println("key: {}"+KEY);
        API = API.replaceAll("<key>", KEY);
    }

    public static double[] addressToGPS(String address) throws IOException {
        try {
            String requestUrl = API.replaceAll("<address>", URLEncoder.encode(address, "UTF-8"));
            System.out.println("请求地址: {}" + requestUrl);
            requestUrl = HttpClientHelper.INSTANCE.get(requestUrl);
            if (requestUrl != null ) {
                Matcher matcher = pattern.matcher(requestUrl);
                if (matcher.find() && matcher.groupCount() == 2) {
                    double[] gps = new double[2];
                    gps[0] = Double.valueOf(matcher.group(1));
                    gps[1] = Double.valueOf(matcher.group(2));
                    System.out.println("gps: {}" + Arrays.toString(gps));
                    return gps;
                }
            }
        } catch (Exception e) {
            throw new BeanOperationException(e);
        }

        return null;
    }

    public static void main(String[] args) {
        try {

            System.out.println(GaodeHelper.addressToGPS("北京西城区"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
